/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.colswe.solestac.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.colswe.solestac.entity.Student;

/**
 *
 * @author David Cendales
 */
public class StudentDao {
    
    /**Update a Student.
     * @param u The student object that will be updated
     * @param em EntityManager object to make the operation
     */
    public void update(final Student u, final EntityManager em) {
        em.merge(u);
    }

    /**Delet a student.
     * @param u The student object that will be deleted
     * @param em EntityManager object to make the operation
     */
    public void delete(final Student u, final EntityManager em) {
        em.remove(em.merge(u));
    }

    /**Create a nuew student.
     * @param u The student object that will be created
     * @param em EntityManager object to make the operation
     */
    public void create(final Student u, final EntityManager em) {
        em.persist(u);
    }
    
    /**Search a student by idetnfication.
     * @param n Identification number given by the student
     * @param em EntityManager object to make the operation
     * @return The student with the information related to the identification
     * number given
     */
    public List<Student> searchByIdentification(final String n, final EntityManager em) {
        Query q = em.createNamedQuery("Student.findByDocument").setParameter("document", n);
        List<Student> student = new ArrayList<Student>();
        student.add((Student) q.getSingleResult());
        return student;
    }
    
    /**Search all students in the system.
     * @param em EntityManager object to make the operation
     * @return All registered requests
     */
    public List<Student> searchAll(final EntityManager em) {
        Query q = em.createNamedQuery("Student.findAll");
        return (List<Student>) q.getResultList();
    }    
}
